<template>
  <a-modal
    :title="title"
    :width="800"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">

    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="协议类型">
          <j-dict-select-tag placeholder="请输入协议类型" v-decorator="['protocolType', {}]" :triggerChange="true" class="select-box"
                             dict-code="sys_user_protocol_protocol_type"></j-dict-select-tag>
<!--          <a-select placeholder="请输入协议类型" v-decorator="['protocolType', {}]" class="select-box">-->
<!--            <a-select-option value="1">注册协议</a-select-option>-->
<!--            <a-select-option value="2">直播协议</a-select-option>-->
<!--            <a-select-option value="3">用户爆料新闻协议</a-select-option>-->
<!--          </a-select>-->
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="协议内容">
          <!--          <div v-html="editorValue"></div>-->
          <j-editor
            toolbar="undo redo |  formatselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | lists | removeformat"
            v-model="editorValue"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="备注">
          <a-input placeholder="请输入备注" v-decorator="['remark', {}]"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import {httpAction} from '@/api/manage'
  import pick from 'lodash.pick'
  import moment from "moment"
  import JEditor from '@/components/jeecg/JEditor'

  export default {
    name: "SysUserProtocolModal",
    components: {JEditor},
    data() {
      return {
        title: "操作",
        visible: false,
        model: {},
        labelCol: {
          xs: {span: 24},
          sm: {span: 5},
        },
        wrapperCol: {
          xs: {span: 24},
          sm: {span: 16},
        },

        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules: {},
        url: {
          add: "/system/sysUserProtocol/add",
          edit: "/system/sysUserProtocol/edit",
        },
        editorValue: '',
      }
    },
    created() {
    },
    methods: {
      add() {
        this.edit({
          protocolType: "1"
        });
      },
      edit(record) {
        this.form.resetFields();
        if (record) {
          record.protocolType = record.protocolType + "" || ""
        }

        this.model = Object.assign({}, record);
        this.visible = true;

        this.editorValue = record.protocolContent
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model, 'remark', 'protocolType'))
          //时间格式化
        });

      },
      photoModalHandle(images) {
        this.editorValue += '<img src="' + item.materialUrl + '" />'
      },
      close() {
        this.$emit('close');
        this.visible = false;
      },
      handleOk() {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if (!this.model.id) {
              httpurl += this.url.add;
              method = 'post';
            } else {
              httpurl += this.url.edit;
              method = 'put';
            }
            let formData = Object.assign(this.model, values);
            //时间格式化
            formData.protocolContent = that.editorValue
            console.log(formData)
            httpAction(httpurl, formData, method).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit('ok');
              } else {
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
              that.close();
            })


          }
        })
      },
      handleCancel() {
        this.close()
      },


    }
  }
</script>

<style lang="less" scoped>

</style>